package com.example.mystoryapp2.ui.launcher

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import androidx.lifecycle.ViewModelProvider
import com.example.mystoryapp2.ui.main.MainActivity
import com.example.mystoryapp2.R
import com.example.mystoryapp2.contexts.AppContext
import com.example.mystoryapp2.data.network.NetCallBack
import com.example.mystoryapp2.data.network.ServiceCreator
import com.example.mystoryapp2.data.network.model.StoryModel
import com.example.mystoryapp2.data.network.model.UserModel
import com.example.mystoryapp2.data.network.service.UserInfoService
import com.example.mystoryapp2.data.sp.SpUtils
import com.example.mystoryapp2.ui.base.BaseActivity
import com.example.mystoryapp2.databinding.ActivityLauncherLayoutBinding
import com.example.mystoryapp2.ui.user.login.LoginActivity
import retrofit2.Call
import retrofit2.Response


class LauncherActivity : BaseActivity<ActivityLauncherLayoutBinding, LauncherViewModel>() {
    private val TAG = "LauncherActivity"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        SpUtils.initUtils(application)
        StoryModel
        AppContext.application = application
        Handler().postDelayed(Runnable {
            nextPage()
        }, 3000)
    }

    /**
     * 结束闪屏页，跳转到主页
     */
    private fun nextPage() {
        val service = ServiceCreator.create<UserInfoService>()
        var intent: Intent
        service.isValid(SpUtils.getStringValue(SpUtils.JWT))
            .enqueue(object : NetCallBack<Boolean>() {
                override fun onResponse(call: Call<Boolean>, response: Response<Boolean>) {
                    if (response.body() != true) {
                        intent = Intent(this@LauncherActivity, LoginActivity::class.java)
                    }else {
                        intent = Intent(this@LauncherActivity, MainActivity::class.java)
                        db.viewModel?.loadUserinfo(object:UserModel.ICallBack{
                            override fun onLoaded() {
                                startActivity(intent);
                                finish()
                            }

                            override fun onFailed() {
                                intent = Intent(this@LauncherActivity, LoginActivity::class.java)
                                startActivity(intent);
                                finish()
                            }
                        })
                    }
                }

                override fun onFailure(call: Call<Boolean>, t: Throwable) {
                    intent = Intent(this@LauncherActivity, LoginActivity::class.java)
                    startActivity(intent);
                    finish()
                }
            })


    }

    override fun getLayoutId(): Int {
        return R.layout.activity_launcher_layout
    }

    override fun getViewModel(): LauncherViewModel {
        return ViewModelProvider(
            this, ViewModelProvider
                .AndroidViewModelFactory(application)
        )[LauncherViewModel::class.java]
    }

    override fun setViewModel() {
        db.viewModel = getViewModel();
    }
}